package com.suneast.suneaststore.ui.fragments.bottom;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.suneast.suneaststore.R;
import com.suneast.suneaststore.ui.fragments.BaseFragment;
import com.suneast.suneaststore.ui.customview.NormalItemView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;
import me.yokeyword.fragmentation.ISupportFragment;

public abstract class AbsBottomFragment extends BaseFragment implements OnTabItemSelectedListener {

    @Override
    public @LayoutRes
    int setLayout() {
        return R.layout.fragment_bottom;
    }

    public abstract LinkedHashMap<BottomTabBean, BottomItemFragment> setItems(ItemBuilder builder);

    private final ArrayList<BottomTabBean> TAB_BEANS = new ArrayList<>();
    private final ArrayList<BottomItemFragment> ITEM_DELEGATES = new ArrayList<>();
    private final LinkedHashMap<BottomTabBean, BottomItemFragment> ITEMS = new LinkedHashMap<>();
    private int mCurrentDelegate = 0;

    private NavigationController mTabController;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ItemBuilder builder = ItemBuilder.builder();
        final LinkedHashMap<BottomTabBean, BottomItemFragment> items = setItems(builder);
        ITEMS.putAll(items);
        for (Map.Entry<BottomTabBean, BottomItemFragment> item : ITEMS.entrySet()) {
            final BottomTabBean key = item.getKey();
            final BottomItemFragment value = item.getValue();
            TAB_BEANS.add(key);
            ITEM_DELEGATES.add(value);
        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        final int size = ITEMS.size();
        final ISupportFragment[] delegateArray = ITEM_DELEGATES.toArray(new ISupportFragment[size]);
        getSupportDelegate().loadMultipleRootFragment(R.id.bottom_bar_delegate_container,
                0, delegateArray);

        PageNavigationView.CustomBuilder builder = ((PageNavigationView)rootView
                .findViewById(R.id.bottom_tab)).custom();
        for (BottomTabBean bean : TAB_BEANS) {
            builder.addItem(newItem(bean.getNormalIcon(), bean.getSelectIcon(),
                    bean.getTitle(), rootView.getContext()));
        }
        mTabController = builder.build();
        mTabController.addTabItemSelectedListener(this);
    }

    //创建一个Item
    private BaseTabItem newItem(int drawable, int checkedDrawable, String text, Context context) {
        NormalItemView normalItemView = new NormalItemView(context);
        normalItemView.initialize(drawable, checkedDrawable, text);
        normalItemView.setTextDefaultColor(getResources().getColor(R.color.color_tab_normal));
        normalItemView.setTextCheckedColor(getResources().getColor(R.color.color_tab_select));
        return normalItemView;
    }

    @Override
    public void onSelected(int index, int old) {
        getSupportDelegate().showHideFragment(ITEM_DELEGATES.get(index), ITEM_DELEGATES.get(mCurrentDelegate));
        //注意先后顺序
        mCurrentDelegate = index;
    }

    @Override
    public void onRepeat(int index) {

    }

    public void setMessageNumber(int index, int number) {
        if (mTabController != null) {
            mTabController.setMessageNumber(index, number);
        }
    }

    public void setHasMessage(int index, boolean hasMessage) {
        if (mTabController != null) {
            mTabController.setHasMessage(index, hasMessage);
        }
    }

}
